package com.airnews.ticket.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.model.Menu;

public class GridAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Menu> mainMenuList;
	private LayoutInflater mInflater;

	public GridAdapter(Context c, ArrayList<Menu> mainMenuList) {
		this.context = c;
		this.mainMenuList = mainMenuList;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return mainMenuList.size();
	}

	@Override
	public Object getItem(int index) {
		return mainMenuList.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_item, null);
			holder = new GridHolder();
			holder.appImage = (ImageView) convertView.findViewById(R.id.main_menu_icon);
			holder.appName = (TextView) convertView.findViewById(R.id.main_menu_text);
			convertView.setTag(holder);
		} else {
			holder = (GridHolder) convertView.getTag();
		}
		Menu info = mainMenuList.get(index);
		if (info != null) {
			holder.appName.setText(info.item_name);
			holder.appImage.setBackgroundResource(info.item_icon);
			convertView.setId(info.item_icon);
		}
		return convertView;
	}

	private class GridHolder {
		ImageView appImage;
		TextView appName;
	}
}
